<?php
/**
 * 拉卡拉对账服务
 * @author angl
 */

date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/common/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

$db = init_db();

$bill_list = $db->where('pay_result',1)->get("recharge_log");

foreach ($bill_list as $bill){
    $day = date('Y-m-d',strtotime($bill['deal_time']));
    $day_bill = $db->where('create_date',$day)->getOne('lakala_bill');
    if(empty($day_bill)){
        $db->insert('lakala_bill',['create_date'=>$day]);
    }
    $remark = json_decode($bill['remark'],true);
    $db->where('create_date',$day)->update('lakala_bill',[
        'settlement_money'=> $db->inc($bill['amount']),
        'amount'=>$db->inc(1),
        'poundage'=>$db->inc($remark['data']['orderFee'])
    ]);
    logUtil("车商: {$bill['dealer_id']} 充值 {$bill['amount']} 成功");
}

logUtil("Task finished");
